﻿using System;
using System.Collections.Generic;
using System.Globalization;
using GpgApi;

namespace WgpgCore
{
    public sealed class KeyNodeFilter
    {
        public String Expression = null;
        public Boolean PublicKeys = true;
        public Boolean SecretKeys = true;

        public Boolean Match(KeyNode key)
        {
            if (key == null || key.Type == KeyType.None || (!PublicKeys && !SecretKeys))
                return false;

            if (key.Type != KeyType.Pair)
            {
                if (key.Type == KeyType.Public && !PublicKeys)
                    return false;

                if (key.Type == KeyType.Secret && !SecretKeys)
                    return false;
            }

            if (String.IsNullOrEmpty(Expression))
                return true;

            List<String> parts = new List<String> { key.Id, key.FingerPrint };

            foreach (KeyUserInfoNode userInfo in key.UserInfos)
            {
                parts.Add(userInfo.Name);
                parts.Add(userInfo.Email);
                parts.Add(userInfo.Comment);
            }

            String tmp = String.Join(" ", parts).ToUpper(CultureInfo.CurrentCulture);
            return tmp.Contains(Expression.ToUpper(CultureInfo.CurrentCulture));
        }
    }
}
